package com.dx.wx.controller;

import com.dx.wx.service.DxShopService;
import com.ejlchina.data.Array;
import com.ejlchina.data.Mapper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/api/dx/shop")
public class ApiDxShopController {

    @Autowired
    private DxShopService dxShopService;



    @PostMapping("/save")
    public R save(@LoginUserId Long phone,@RequestBody  Map map){
        dxShopService.saveShop(phone, map);
        return R.ok();
    }

    @PostMapping("/check")
    public R check(@LoginUserId Long phone,String id,Integer type){
        dxShopService.checkShop(id, type);
        return R.ok();
    }


    //通过名字搜索客户
    @GetMapping("/name")
    @IgnoreAuth
    public R findByName(String name,String appId){
      Array array =  dxShopService.findShopByName(name,appId);
      return R.ok(array.toString());
    }


    //查找自己拥有的客户
    @GetMapping("/")
    public R findShop(@LoginUserId Long phone){
        Array array =  dxShopService.findShop(phone);
        return R.ok(array.toString());
    }

    @GetMapping("/{id}")
    public R findShop(@LoginUserId Long phone,@PathVariable String id){
        Mapper mapper =  dxShopService.findShopById(id);
        Array prices = dxShopService.findPriceByName(mapper.getString("name"));
        Array tempGoods = dxShopService.findTempGoods(id);
        return R.ok(mapper.toString()).putExtra("prices",prices.toString()).putExtra("tempGoods",tempGoods.toString());
    }

    //查找自己的直接客户
    @GetMapping("/top")
    public R findByTop(@LoginUserId Long phone){
        Array array =  dxShopService.findShopByTop(phone);
        return R.ok(array.toString());
    }

    //查找连锁下面药店
    @PostMapping("/link")
    public R findByLinkShop(@LoginUserId Long phone,String[] linkIds){
        Array array =  dxShopService.findShopByLinkShop(linkIds);
        return R.ok(array.toString());
    }

    //获取价目表
    @GetMapping("/price")
    @IgnoreAuth
    public R findPrice(@LoginUserId Long phone,String shopId){
        Array array = dxShopService.findPrice(phone);
        return R.ok(array);
    }

    //获取可售产品
    @GetMapping("/goods")
    @IgnoreAuth
    public R findGoods(@LoginUserId Long phone){
        Array array = dxShopService.findPrice(phone);
        return R.ok(array);
    }


    //根据药店ID获取
    @GetMapping("/goods/{id}")
    @IgnoreAuth
    public R findGoodsById(@PathVariable String id){
        Array array = dxShopService.findPriceByShopId(id);
        return R.ok(array.toString());
    }


    //更新药店
    @PostMapping("/update")
    public R update(@RequestBody Map map){
        dxShopService.update(map);
        return R.ok();
    }

    //更新药店负责人
    @PostMapping("/update/owner")
    public R updateOwner(String userId,String[] shopIds){
        dxShopService.updateOwner(userId, shopIds);
        return R.ok();
    }




}
